
.PHONY: deploy 
# 需要改的地方
include .env

# 获取当前目录名
CURRENT_DIR_NAME := $(notdir $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST)))))

SSH_CMD =echo $(DEV_SSH_HOST) && ssh -o StrictHostKeyChecking=no -i ${DEV_SSH_KEY}  $(DEV_SSH_HOST)
PROJECT_NAME = $(notdir $(CURDIR))
HOME_DIR =~/$(PROJECT_NAME)


#  前提条件
hk-pre: hk-base-env caddy-ui-env ip

pre: base-env ip caddy-ui-env 

caddy-ui-env:
	 @echo "======重点====="
	 @echo "记得scp Caddyfile 到服务器上"
	 @echo "======重点====="


 
run:
	@echo "当前镜像:" 
	@echo $(IMAGE_NAME)
	@  docker compose -f deploy/docker-compose.yml  --env-file .env  up



base-env: docker-env 

hk-base-env: docker-env-hk ssh-env

	@$(SSH_CMD) "ssh-keygen -t ed25519 -N '' -f ~/.ssh/id_ed25519"
ssh-env:
	@echo "开始检测ssh环境"
	@$(SSH_CMD) "command -v ssh >/dev/null 2>&1 || { echo 'SSH not found, installing...'; yum install -y openssh-server; }"
	@$(SSH_CMD) "ssh-keygen -t ed25519 -N ''"
	@$(SSH_CMD) "cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys"
	@$(SSH_CMD) "cat ~/.ssh/authorized_keys"
	@echo "ssh installed"

caddy-env:
	@echo "======开始检测caddy环镜检测====="
	# @$(SSH_CMD) "command -v caddy >/dev/null 2>&1 || { echo 'Caddy not found, installing...';sudo yum install -y caddy; }"
	@scp -o StrictHostKeyChecking=no -i ${DEV_SSH_KEY}  caddy-install.sh $(DEV_SSH_HOST):~/
	@$(SSH_CMD) "chmod +x ~/caddy-install.sh"
	@$(SSH_CMD) "sudo ./caddy-install.sh"
	@$(SSH_CMD) "caddy version"
	@echo "caddy installed"
	@echo "======结束检测caddy环境检测====="

docker-env-hk:
	@echo "开始检测docker环境"
	@$(SSH_CMD) "mkdir -p /etc/apt/sources.list.d"
	@$(SSH_CMD) "command -v docker >/dev/null 2>&1 ||   curl -fsSL https://get.docker.com | bash -s docker"
	@$(SSH_CMD) "sudo systemctl start docker"
	@$(SSH_CMD) "sudo systemctl enable docker"
	@$(SSH_CMD) "docker -v"
	@echo "docker installed"

docker-env:
	@echo "开始检测docker环境"

	@$(SSH_CMD) "command -v docker >/dev/null 2>&1 || { echo 'Docker not found, installing...'; if [ -f /etc/alinux-release ]; then curl -fsSL http://shell.huthrc.top/docker-centos.sh | bash ; else curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun; fi; }"
	@$(SSH_CMD) "sudo systemctl start docker"
	@$(SSH_CMD) "sudo systemctl enable docker"
	@$(SSH_CMD) "docker -v"
	@echo "docker installed"


deploy: 
	@rsync -avz --exclude='Caddyfile'  -r . $(DEV_SSH_HOST):$(HOME_DIR)
	@$(SSH_CMD) "cd $(HOME_DIR)/deploy &&  docker compose pull && docker compose down && docker compose up -d"
	@make ps
	@make log



# 容器相关

log:
	$(SSH_CMD) "cd $(HOME_DIR)/deploy && docker compose logs -f"

ps:
	$(SSH_CMD) "cd $(HOME_DIR)/deploy && docker compose ps"

ps-a:
	$(SSH_CMD) "docker ps "





## 工具类

ip:
	@$(SSH_CMD) "curl -s ip.me"
	@$(SSH_CMD) "hostname -I | awk '{print $$1}'"

ping:
	@$(SSH_CMD) "ping -c 4 temp.huthrc.top"




## ssh 相关

ssh:
	@$(SSH_CMD) 

# 提取 IP 地址
DEV_IP := $(shell echo $(DEV_SSH_HOST) | cut -d'@' -f2)

ssh-clean:
	ssh-keygen -R  ${DEV_IP}

scp:
	  @$(SSH_CMD) "mkdir -p $(HOME_DIR)/$(CURRENT_DIR_NAME) && cd $(HOME_DIR)/$(CURRENT_DIR_NAME) && echo '当前目录: ' && pwd"
		scp -r  $(name)  $(DEV_SSH_HOST):$(HOME_DIR)/$(CURRENT_DIR_NAME)

sh:
	$(SSH_CMD) $(cmd)


## 资源监控
# 查看资源使用前10
n?=10
TOP_CMD = "ps -eo pid,%cpu,%mem,rss,vsz,cmd --sort -%mem | awk 'NR==1 { printf \"%-10s %-10s %-10s %-10s %-10s %s\\n\", \$$1, \$$2, \$$3, \"RSS(MB)\", \"VSZ(MB)\", \$$6 } NR>1 { printf \"%-10s %-10s %-10s %-10.2f %-10.2f %s\\n\", \$$1, \$$2, \$$3, (\$$4/1024), (\$$5/1024), \$$6 }'"

top:
	@$(SSH_CMD) ${TOP_CMD} | head -n $$(($(n) + 1))

CPU_CMD = "ps -eo pid,%cpu,%mem,rss,vsz,cmd --sort -%cpu | awk 'NR==1 { printf \"%-10s %-10s %-10s %-10s %-10s %s\\n\", \$$1, \$$2, \$$3, \"RSS(MB)\", \"VSZ(MB)\", \$$6 } NR>1 { printf \"%-10s %-10s %-10s %-10.2f %-10.2f %s\\n\", \$$1, \$$2, \$$3, (\$$4/1024), (\$$5/1024), \$$6 }'"

top-p:
	@$(SSH_CMD) ${CPU_CMD} | head -n $$(($(n) + 1))


#内存总体使用情况
mem:
	@$(SSH_CMD) "echo 'Memory Usage:' && free -m | awk '/Mem/ { printf(\"%sM/%sM (%.2f%%)\\n\", \$$3, \$$2, (\$$3/\$$2)*100) }' "
# CPU总体使用情况

cpu:
	@$(SSH_CMD) "ps -A -o %cpu | awk '{s+=\$$1} END {print \"CPU 使用率: \"s\"%\"}'"
#磁盘总体使用情况
disk:
	@$(SSH_CMD) 'echo "根目录磁盘使用情况:" && echo "-----------------------------------" && \
	df -h | grep -E "^.* /$$" | awk '\''{ printf "总容量: %-10s 已用: %-10s 可用: %-10s 使用率: %s\n", $$2, $$3, $$4, $$5 }'\'' && \
	echo "-----------------------------------"'
#网络使用情况
net:
	@$(SSH_CMD) 'echo "网络接口使用情况:" && echo "-----------------------------------" && \
	ifconfig | awk '\''BEGIN {OFS="\t"} /^[a-zA-Z0-9]/ {iface=$$1} /bytes/ {if($$0 ~ /RX packets/) {rx=$$5} else if($$0 ~ /TX packets/) {tx=$$5; printf "接口: %-8s 接收: %-5.2fMB 发送: %-5.2fMB\n", iface, (rx/1024/1024), (tx/1024/1024)} }'\'' && \
	echo "-----------------------------------"'
